<?php 
require('./lib/init.php');
$art_id = $_GET['art_id'];


//判断地址栏传过来的art_id是否合法
if(!is_numeric($art_id)){
	header('Location:index.php');
}

//如果没有这篇文章,同样跳转到首页
$sql = "select * from art where art_id=$art_id";
if(!mGetRow($sql)){
	header('Location:index.php');
}

//查询文章
$sql = "select * from art inner join cat on cat.cat_id=art.cat_id where art_id=$art_id";
$arts = mGetAll($sql);
//print_r($arts);

if(!empty($_POST)){
	$comm['nick'] = trim($_POST['nick']);
	$comm['email'] = trim($_POST['email']);
	$comm['content'] = htmlspecialchars(trim($_POST['content']));
	$comm['pubtime'] = time();
	$comm['art_id'] = $art_id;
	$comm['ip'] = sprintf('%u',ip2long(getRealIp()));//看不懂到底是什么玩意
	$rs = mExec('comment',$comm);
	//var_dump($rs);
	//每增加一条评论,art表的comm字段要加+1
	$sql = "update art set comm=comm+1 where art_id=$art_id";
	mQuery($sql);
	if($rs){
		$ref = $_SERVER['HTTP_REFERER'];
		header("Location:$ref");
	}
}

//留言查询
$sql = "select * from  comment where art_id=$art_id";
$comms = mGetAll($sql);
//print_r($comms);
require(ROOT.'/view/front/art.html');
